﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using SharePointOfView.SPReporting.Jobs;
using System.Xml;
using System.IO;

namespace SharePointOfView.SPReporting.JobConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] dllFiles = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.dll");
            foreach (string assemblyFile in dllFiles)
            {
                Assembly assbly = Assembly.LoadFrom(assemblyFile);
                Console.WriteLine("Loading of " + assemblyFile);

                Module[] mods = assbly.GetLoadedModules(false);
                foreach (Module mod in mods)
                {
                    Console.WriteLine("  [Module] " + mod.FullyQualifiedName);
                    foreach (Type type in mod.GetTypes())
                    {
                        Console.Write("\t[Type] " + type.Name);
                        if (type.IsSubclassOf(typeof(GenericReportingJobDefinition)))
                        {
                            Console.WriteLine("(is GenericReportingJobDefinition)");

                            object job = type.InvokeMember(type.Name, BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.CreateInstance, null, type, null);
                            if (job is GenericReportingJobDefinition)
                            {
                                XmlDocument xDoc = ((GenericReportingJobDefinition)job).GatherData();
                                xDoc.Save(type.Name + ".xml");
                                Console.WriteLine("\t[XML] Data saved to " + type.Name + ".xml");
                            }
                        }
                        else 
                            Console.WriteLine();
                    }
                }
            }
        }
    }
}
